package com.biubiu.biubiuexam.services;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.biubiu.biubiuexam.common.dto.CreateChoiceQuestionDto;
import com.biubiu.biubiuexam.common.dto.CreateSaQuestionDto;
import com.biubiu.biubiuexam.common.dto.TeacherExamQuestionDto;
import com.biubiu.biubiuexam.common.lang.Result;
import com.biubiu.biubiuexam.nosql.model.ExamQuestion;
import com.biubiu.biubiuexam.nosql.repository.ExamQuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * The type Question service.
 */
@Service
public class QuestionService {

    private final ExamQuestionRepository examQuestionRepository;

    /**
     * Instantiates a new Question service.
     *
     * @param examQuestionRepository the exam question repository
     */
    @Autowired
    public QuestionService(ExamQuestionRepository examQuestionRepository) {
        this.examQuestionRepository = examQuestionRepository;
    }

    private Result createQuestion(int type, JSONObject content, Long teacherId) {
        ExamQuestion examQuestion = new ExamQuestion();
        examQuestion.setId(IdUtil.objectId());
        examQuestion.setType(type);
        examQuestion.setContent(content);
        examQuestion.setLink(IdUtil.objectId());
        examQuestion.setTeacherId(teacherId);
        examQuestionRepository.save(examQuestion);

        TeacherExamQuestionDto temp = new TeacherExamQuestionDto();
        temp.setType(type);
        temp.setContent(content);
        temp.setLink(examQuestion.getLink());

        return Result.success(temp);
    }

    /**
     * Create choice question result.
     *
     * @param teacherId         the teacher id
     * @param choiceQuestionDto the choice question dto
     * @return the result
     */
    public Result createChoiceQuestion(Long teacherId, CreateChoiceQuestionDto choiceQuestionDto) {
        JSONObject content = JSONUtil.createObj();
        content.set("description", choiceQuestionDto.getDescription());
        content.set("options", choiceQuestionDto.getOptions());
        content.set("answer", choiceQuestionDto.getAnswer());
        return createQuestion(0, content, teacherId);
    }

    /**
     * Create sa question result.
     *
     * @param teacherId           the teacher id
     * @param createSaQuestionDto the create sa question dto
     * @return the result
     */
    public Result createSaQuestion(Long teacherId, CreateSaQuestionDto createSaQuestionDto) {
        JSONObject content = JSONUtil.createObj();
        content.set("description", createSaQuestionDto.getDescription());
        content.set("answer", createSaQuestionDto.getAnswer());
        return createQuestion(1, content, teacherId);
    }

    /**
     * Delete question result.
     *
     * @param teacherId the teacher id
     * @param link      the link
     * @return the result
     */
    public Result deleteQuestion(Long teacherId, String link) {
        ExamQuestion examQuestion = examQuestionRepository.findExamQuestionByLink(link);
        if (examQuestion == null) {
            return Result.fail(404, "没有对应试题！", null);
        }
        if (!Objects.equals(examQuestion.getTeacherId(), teacherId)) {
            return Result.fail(401, "该试题不是您创建的！", null);
        }

        examQuestionRepository.delete(examQuestion);
        return Result.success(null);
    }

    /**
     * Gets my all questions.
     *
     * @param teacherId the teacher id
     * @return the my all questions
     */
    public Result getMyAllQuestions(Long teacherId) {
        List<ExamQuestion> list = examQuestionRepository.findExamQuestionAllByTeacherId(teacherId);
        List<TeacherExamQuestionDto> reList = new ArrayList<>();
        for (ExamQuestion item : list) {
            TeacherExamQuestionDto temp = new TeacherExamQuestionDto();
            temp.setType(item.getType());
            temp.setLink(item.getLink());
            temp.setContent(item.getContent());
            reList.add(temp);
        }
        return Result.success(reList);
    }
}
